//
//  EditMyBankCardViewController.m
//  lmq
//
//  Created by 马田成 on 2017/3/22.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "EditMyBankCardViewController.h"
#import "cashViewController.h"

@interface EditMyBankCardViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *cardTextField;
@property (weak, nonatomic) IBOutlet UILabel *cardTypeLabel;
@property (weak, nonatomic) IBOutlet UITextField *identityCardTextField;

@end

@implementation EditMyBankCardViewController
- (void)viewWillAppear:(BOOL)animated{

    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
    [productParams setValue:tokenStr forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:My_BankCardKey
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       NSDictionary *dict1=[returnData objectForKey:@"data"];
                                       ;
                                       //                                       self.appkey = [dict1 objectForKey:@"appkey"];
                                       
                                       
                                       [PublicFunction saveConfig:[dict1 objectForKey:@"appkey"] forKey:@"appkey"];
                                       
                                   }
                                   else {
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];


}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationView];
    
    
    // Do any additional setup after loading the view from its nib.
}
- (void)creationView{

    [self.cardTextField addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged ];
    
}
- (IBAction)confirm:(UIButton *)sender {
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *bankName=[PublicFunction getConfigWithKey:@"bankname"];
    
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
    [productParams setValue:tokenStr forKey:@"token"];
    [productParams setValue:bankName forKey:@"bank_name"];
    [productParams setValue:self.nameTextField.text forKey:@"full_name"];
    [productParams setValue:self.cardTextField.text forKey:@"bank_num"];
    [productParams setValue:self.identityCardTextField.text forKey:@"id_card"];
    
    
    
    
    [[MNetworkHandler sharedInstance]conURL:My_RealName
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       cashViewController *vc = [[cashViewController alloc] init];
//                                       [self.navigationController pushViewController:vc animated:YES];
                                       [self.navigationController popViewControllerAnimated:YES];
                                       
                                       
                                       
                                       
                                   }
                                   else{
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
    
    
    

    
}
-(void)textFieldDidChange{
    
    if (self.cardTextField.text.length == 19) {
             
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"appkey"];


    [productParams setValue:self.cardTextField.text forKey:@"cardnum"];
    [productParams setValue:tokenStr forKey:@"key"];
        
    [[MNetworkHandler sharedInstance]conURL:@"http://api.avatardata.cn/Bank/Query"
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"error_code"]intValue]==0) {
                                       
                                       NSDictionary *dict1=[returnData objectForKey:@"result"];
                                       ;
                                       self.cardTypeLabel.text = [dict1 objectForKey:@"bankname"];
                                       [PublicFunction saveConfig:[dict1 objectForKey:@"bankname"] forKey:@"bankname"];
                                       
                                       
                                       
                                       NSLog(@"%@", dict1);
                                       

                                   }
                                   else {
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
    

    
    }else {
    
        
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.cardTextField resignFirstResponder];
    [self.nameTextField resignFirstResponder];
    [self.identityCardTextField resignFirstResponder];
    

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
